package com.hanbang.gycatering.order;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hanbang.gangyu.R;
import com.hanbang.gycatering.adpater.OrderAdapter;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.bean.CommodityData;
import com.hanbang.gycatering.bean.OrderData;
import com.hanbang.gycatering.bean.PropertyBeanData;
import com.hanbang.gycatering.bean.TwoLevelData;
import com.hanbang.gycatering.bean.UserInforData;
import com.hanbang.gycatering.homepage.BaseActivity;
import com.hanbang.gycatering.homepage.BaseFragmentActivity;
import com.hanbang.gycatering.property.FixRecord;
import com.hanbang.gycatering.property.FixRecord.Myadapter;
import com.hanbang.gycatering.utils.BitmapUtil;
import com.hanbang.gycatering.utils.DbUtil;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyLog;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.MySharedPreferences;
import com.hanbang.gycatering.utils.MyToast;
import com.hanbang.gycatering.utils.PagingHelp;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.exception.DbException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class WaitDelivery extends Fragment {
	private PullToRefreshListView ao_listview;
	private ArrayList<OrderData> orderDatas = new ArrayList<OrderData>();
	private ArrayList<ArrayList<CommodityData>> ordertwoDatas = new ArrayList<ArrayList<CommodityData>>();
	private OrderAdapter myadapter;
	private LinearLayout ll_no_order;
	private View rootView = null;
	public UserInforData userData = null;
	private PagingHelp pagingHelp = new PagingHelp();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		userData = ((MyApplication) getActivity().getApplication()).userData;
		getDbUserData();
	}
	public void getDbUserData() {
		int id = MySharedPreferences.getSharedPreferencesKeyAndValue(getActivity(),
				MySharedPreferences.USER_DATA, "user_id", -1);
		if (id != -1) {
			try {
				userData = DbUtil.getDbUtils(getActivity()).findById(
						UserInforData.class, id);
			} catch (DbException e) {
				e.printStackTrace();
				userData = null;
			}
		} else {
			userData = null;
		}
	}
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// 加载/res/layout/目录下的fragment_carbon.xml布局文件
		rootView = inflater
				.inflate(R.layout.allorder, container, false);
		MyLog.e("ActGroupDemo", "Act1 onCreate()");
			initView();
			getOrderDatas();
		return rootView;
	}
	 

	private void initView() {
		// TODO Auto-generated method stub
		ao_listview = (PullToRefreshListView) rootView.findViewById(R.id.order_listview);
		ll_no_order = (LinearLayout) rootView.findViewById(R.id.ll_no_order);
//		myadapter = new OrderAdapter(getActivity(), orderDatas, ordertwoDatas);
		ao_listview.setAdapter(myadapter);
		ao_listview.setMode(Mode.BOTH);
		ao_listview.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				MyToast.show(getActivity(), "你点击了" + arg2, Toast.LENGTH_SHORT);
			}
		});
		ao_listview.setOnRefreshListener(new OnRefreshListener2<ListView>() {
			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				// TODO Auto-generated method stub
				orderDatas.clear();
				ordertwoDatas.clear();
				pagingHelp.clear();
				getOrderDatas();
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase<ListView> refreshView) {
				// TODO Auto-generated method stub
				getOrderDatas();
			}
		});
	}

	// 状态（type）：0.全部订单；1.待付款；2.待发货；3.待收货；5.已完成
	private void getOrderDatas() {
		// TODO Auto-generated method stub
		((MyApplication) getActivity().getApplicationContext()).httpUtils.send(
				HttpMethod.GET,
				HttpInterfaces.myorders + "&userphone="
						+  userData.getUserphone() + "&password="
						+  userData.getUserpassword() + "&PageIndex="
						+ pagingHelp.getPageindex() + "&type=2",
				new MyRequestCallBack<String>(getActivity(),
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								pagingHelp.setRecordCount(json
										.getInt("recordCount"));
								pagingHelp.setPageCount(json
										.getInt("pageCount"));
								JSONArray infor = json.getJSONArray("info");
								int i = 0;
								for (i = pagingHelp.getCurrentCount(); i < infor
										.length(); i++) {
									OrderData pbdata = new OrderData();
									pbdata.setOrdernumber(infor
											.getJSONObject(i).getString(
													"orderid"));
									pbdata.setOrderstate(infor.getJSONObject(i)
											.getInt("state"));
									pbdata.setTotleprice(infor.getJSONObject(i)
											.getDouble("totleprice"));
									pbdata.setOdercount(infor.getJSONObject(i)
											.getInt("totlecount"));
									JSONArray more = infor.getJSONObject(i)
											.getJSONArray("content");
									ArrayList<CommodityData> twoS = new ArrayList<CommodityData>();
									for (int j = 0; j < more.length(); j++) {
										CommodityData commdata = new CommodityData();
										commdata.setId(more.getJSONObject(j)
												.getInt("id"));
										commdata.setName(more.getJSONObject(j)
												.getString("name"));
										commdata.setImg(more.getJSONObject(j)
												.getString("img"));
										commdata.setPrice(more.getJSONObject(j)
												.getString("price"));
										commdata.setCount(more.getJSONObject(j)
												.getInt("count"));
										twoS.add(commdata);
									}
									ordertwoDatas.add(twoS);
									pbdata.setColl(ordertwoDatas);
									orderDatas.add(pbdata);
								}
								MyLog.e("getView", orderDatas.toString());
								pagingHelp.isNextPaging(getActivity(),
										infor.length(), i);
							} else {

							}
							MyLog.e("initData", orderDatas.size() + "");
							myadapter.notifyDataSetChanged();
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(getActivity(), "获取数据失败",
									Toast.LENGTH_SHORT);
						}
						if (orderDatas.size() == 0) {
							ao_listview.setVisibility(View.GONE);
							ll_no_order.setVisibility(View.VISIBLE);
						} else {
							MyLog.e("1", myadapter.getCount() + "");
							ao_listview.setVisibility(View.VISIBLE);
							ll_no_order.setVisibility(View.GONE);
						}
						ao_listview.onRefreshComplete();
					}
				});

	}
}
